package com.java1234.rocketmq;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * 消息生产者service类
 * @author java1234_小锋
 * @site www.java1234.com
 * @company 南通小锋网络科技有限公司
 * @create 2021-09-05 11:50
 */
@Component("producerService")
public class ProducerService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

//    /**
//     * 发送简单消息
//     */
//    public void sendMessage(){
//        for(int i=0;i<10;i++){
//            rocketMQTemplate.convertAndSend("java1234-rocketmq","rocketmq大爷，你好2"+i);
//        }
//    }


//    public void sendAsyncMessage(){
//        for(int i=0;i<10;i++){
//            rocketMQTemplate.asyncSend("java1234-rocketmq", "rocketmg异步消息！"+i,
//                    new SendCallback() {
//                        @Override
//                        public void onSuccess(SendResult sendResult) {
//                            System.out.println("发送成功！");
//                        }
//                        @Override
//                        public void onException(Throwable throwable) {
//                            System.out.println("发送失败！");
//                        }
//                    });
//        }
//    }

    /**
     * 发送同步顺序消息
     */
//    public void sendOrderlyMessage(){
//// hashKey用来计算决定消息发送到哪个消息队列 一般是订单ID，产品ID等
//        rocketMQTemplate.syncSendOrderly("java1234-rocketmq-orderly", "98456231,创建", "98456231");
//        rocketMQTemplate.syncSendOrderly("java1234-rocketmq-orderly", "98456231,支付", "98456231");
//        rocketMQTemplate.syncSendOrderly("java1234-rocketmq-orderly", "98456231,完成", "98456231");
//        rocketMQTemplate.syncSendOrderly("java1234-rocketmq-orderly", "98456232,创建", "98456232");
//        rocketMQTemplate.syncSendOrderly("java1234-rocketmq-orderly", "98456232,支付", "98456232");
//        rocketMQTemplate.syncSendOrderly("java1234-rocketmq-orderly", "98456232,完成", "98456232");
//    }

    /**
     * 发送延迟消息
     */
//    public void sendDelayMessage(){
//        rocketMQTemplate.syncSend("java1234-rocketmq", MessageBuilder.withPayload("rocketmq延迟1秒消息").build(),3000,1);
//        rocketMQTemplate.syncSend("java1234-rocketmq",MessageBuilder.withPayload("rocketmq延迟5秒消息").build(),3000,2);
//        rocketMQTemplate.syncSend("java1234-rocketmq",MessageBuilder.withPayload("rocketmq延迟30秒消息").build(),3000,4);
//    }

    /**
     * 发送带Tag消息，测试根据Tag过滤消息
     */
//    public void sendMessageWithTag() {
//// 构造消息1
//        Message msg1 = MessageBuilder.withPayload("rocketmq过滤消息测试-TAG01").build();
//// 构造消息2
//        Message msg2 = MessageBuilder.withPayload("rocketmq过滤消息测试-TAG02").build();
//// 构造消息3
//        Message msg3 = MessageBuilder.withPayload("rocketmq过滤消息测试-TAG03").build();
//        rocketMQTemplate.convertAndSend("java1234-filter-rocketmq" + ":" + "TAG1",
//                msg1);
//        rocketMQTemplate.convertAndSend("java1234-filter-rocketmq" + ":" + "TAG2",
//                msg2);
//        rocketMQTemplate.convertAndSend("java1234-filter-rocketmq" + ":" + "TAG3",
//                msg3);
//    }

    /**
     * 发送事务消息
     */
    public void sendTransactionMessage(){
        // 构造消息
        Message msg = MessageBuilder.withPayload("rocketmq事务消息-01").build();
        rocketMQTemplate.sendMessageInTransaction("java1234-transactionRocketmq",msg,null);
    }

}
